<?php


namespace app\admin\system\controller;


use app\admin\system\logic\DictTypeLogic;
use app\common\controller\BaseAdminController;

class DictTypeController extends BaseAdminController
{
    protected $noNeedRight = ['*'];

    protected $idKey = 'id';

    protected function init()
    {
        $this->logic = new DictTypeLogic();
        parent::init();
    }

    protected function searchWhere()
    {
        $where = [];
        $this->form['name'] && $where[] = ['name', 'like', "%{$this->form['name']}%"];
        $this->form['type'] && $where[] = ['type', '=', $this->form['type']];
        $this->form['status'] && $where[] = ['status', '=', $this->form['status']];
        return $where;
    }

    public function listDictType()
    {
        $dictStatus = input('status');
        $where = [];
        if (!is_null($dictStatus)) {
            $where[] = ['status', '=', $dictStatus];
        }
        $data = $this->logic->where($where)->select();
        return $this->success($data);
    }

    public function read()
    {
        $model = $this->logic->find(request()->input('id'));
        if ($model) {
            return $this->success($model->toArray());
        } else {
            return $this->error('未查找到信息');
        }
    }

    public function remove()
    {
        $ids = request()->input($this->idsKey, [request()->input($this->idKey)]);
        if (!empty($ids)) {
            $this->logic->where([['id', 'in', $ids]])->delete();
            $data = [
                'ids' => $ids
            ];
            return $this->success();
        } else {
            return $this->error('参数错误，请检查');
        }
    }
}